<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>快速入门 | minigame-canvas-engine</title>
    <meta name="description" content="轻量级canvas渲染引擎">
    <meta name="generator" content="VuePress 1.3.1">
    <meta name="robots" content="all">
  <meta name="author" content="yuanzm">
  <meta name="keywords" content="minigame-canvans-engine, minigame, canvas, engine, game engine">
    
    <link rel="preload" href="/minigame-canvas-engine/assets/css/0.styles.cc9e188e.css" as="style"><link rel="preload" href="/minigame-canvas-engine/assets/js/app.34da408a.js" as="script"><link rel="preload" href="/minigame-canvas-engine/assets/js/2.b58d2eb8.js" as="script"><link rel="preload" href="/minigame-canvas-engine/assets/js/7.6ddd2960.js" as="script"><link rel="prefetch" href="/minigame-canvas-engine/assets/js/10.525d02f5.js"><link rel="prefetch" href="/minigame-canvas-engine/assets/js/3.65046f77.js"><link rel="prefetch" href="/minigame-canvas-engine/assets/js/4.e50764d8.js"><link rel="prefetch" href="/minigame-canvas-engine/assets/js/5.aa96439f.js"><link rel="prefetch" href="/minigame-canvas-engine/assets/js/6.8bafe2e6.js"><link rel="prefetch" href="/minigame-canvas-engine/assets/js/8.5202cd3c.js"><link rel="prefetch" href="/minigame-canvas-engine/assets/js/9.b0c2371a.js">
    <link rel="stylesheet" href="/minigame-canvas-engine/assets/css/0.styles.cc9e188e.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/minigame-canvas-engine/" class="home-link router-link-active"><!----> <span class="site-name">minigame-canvas-engine</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"> <a href="https://github.com/wechat-miniprogram/minigame-canvas-engine" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"> <a href="https://github.com/wechat-miniprogram/minigame-canvas-engine" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav>  <ul class="sidebar-links"><li><a href="/minigame-canvas-engine/" class="sidebar-link">概览</a></li><li><section class="sidebar-group depth-0"><a href="/minigame-canvas-engine/api/guide" class="sidebar-heading clickable open active"><span>文档</span> <!----></a> <ul class="sidebar-links sidebar-group-items"><li><a href="/minigame-canvas-engine/api/guide.html" class="active sidebar-link">快速入门</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/minigame-canvas-engine/api/guide.html#安装" class="sidebar-link">安装</a></li><li class="sidebar-sub-header"><a href="/minigame-canvas-engine/api/guide.html#极简示例" class="sidebar-link">极简示例</a></li></ul></li><li><a href="/minigame-canvas-engine/api/api.html" class="sidebar-link">API</a></li><li><a href="/minigame-canvas-engine/api/tags.html" class="sidebar-link">标签</a></li><li><a href="/minigame-canvas-engine/api/style.html" class="sidebar-link">样式</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="快速入门"><a href="#快速入门" class="header-anchor">#</a> 快速入门</h1> <h2 id="安装"><a href="#安装" class="header-anchor">#</a> 安装</h2> <p>克隆本项目到合适的文件夹或者通过Bower安装。</p> <div class="language-shell extra-class"><pre class="language-shell"><code>// git安装方式
<span class="token function">git</span> clone https://github.com/wechat-miniprogram/minigame-canvas-engine
// npm安装方式
bower <span class="token function">install</span> minigame-canvas-engine
</code></pre></div><h2 id="极简示例"><a href="#极简示例" class="header-anchor">#</a> 极简示例</h2> <ol><li>引用渲染引擎：</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> Layout <span class="token keyword">from</span> <span class="token string">'minigame-canvas-engine'</span>
</code></pre></div><ol start="2"><li>编写模板字符串：这里采用XML去描述界面，而且支持有限的标签。需要特别注意的是，模板字符串只能有一个根节点，第二个节点会被忽略。如果想让模板更加强大，可以借助第三方模板引擎如<a href="https://olado.github.io/doT/index.html" target="_blank" rel="noopener noreferrer">dot.js<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>。</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">let</span> template <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">
    &lt;view id=&quot;container&quot;&gt;
    &lt;text id=&quot;testText&quot; class=&quot;redText&quot; value=&quot;hello canvas&quot;&gt;
    &lt;/view&gt;
</span><span class="token template-punctuation string">`</span></span>
</code></pre></div><ol start="3"><li>编写样式：样式为一个style对象，与Web开发不同的是，不受属性前后顺序的影响，class的属性会覆盖id的同名属性。</li></ol> <div class="custom-block tip"><p class="custom-block-title">TIP</p> <p>由于采用了第三方的布局引擎css-layout，在编写样式的时候为所有元素都设置好width和height属性会更容易得到想要的效果，其中<strong>根节点</strong>必须设置width和height属性。</p></div> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">let</span> style <span class="token operator">=</span> <span class="token punctuation">{</span>
    container<span class="token operator">:</span> <span class="token punctuation">{</span>
        width<span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
        height<span class="token operator">:</span> <span class="token number">100</span><span class="token punctuation">,</span>
        backgroundColor<span class="token operator">:</span> <span class="token string">'#ffffff'</span><span class="token punctuation">,</span>
        justContent<span class="token operator">:</span> <span class="token string">'center'</span><span class="token punctuation">,</span>
        alignItems<span class="token operator">:</span> <span class="token string">'center'</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    testText<span class="token operator">:</span> <span class="token punctuation">{</span>
        color<span class="token operator">:</span> <span class="token string">'#ffffff'</span><span class="token punctuation">,</span>
        width<span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
        height<span class="token operator">:</span> <span class="token number">50</span><span class="token punctuation">,</span>
        lineHeight<span class="token operator">:</span> <span class="token number">50</span><span class="token punctuation">,</span>
        fontSize<span class="token operator">:</span> <span class="token number">20</span><span class="token punctuation">,</span>
        textAlign<span class="token operator">:</span> <span class="token string">'center'</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token comment">// 文字的最终颜色为#ff0000</span>
    redText<span class="token operator">:</span> <span class="token punctuation">{</span>
        color<span class="token operator">:</span> <span class="token string">'#ff0000'</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><ol start="4"><li>初始化渲染引擎：将第二第三步编写的模板和样式传给渲染引擎，渲染引擎会生成布局树和渲染树等，准备渲染到canvas上面。</li></ol> <div class="language-js extra-class"><pre class="language-js"><code>Layout<span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span>template<span class="token punctuation">,</span> style<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="5"><li>执行渲染：指定被渲染的context，绘制UI</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">let</span> canvas <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">'canvas'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> context <span class="token operator">=</span> canvas<span class="token punctuation">.</span><span class="token function">getContext</span><span class="token punctuation">(</span><span class="token string">'2d'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 设置canvas的尺寸和样式的container比例一致</span>
canvas<span class="token punctuation">.</span>style<span class="token punctuation">.</span>width <span class="token operator">=</span> <span class="token number">200</span> <span class="token operator">+</span> <span class="token string">'px'</span><span class="token punctuation">;</span>
canvas<span class="token punctuation">.</span>style<span class="token punctuation">.</span>height <span class="token operator">=</span> <span class="token number">100</span> <span class="token operator">+</span> <span class="token string">'px'</span><span class="token punctuation">;</span>
canvas<span class="token punctuation">.</span>width <span class="token operator">=</span> <span class="token number">400</span><span class="token punctuation">;</span>
canvas<span class="token punctuation">.</span>height <span class="token operator">=</span> <span class="token number">200</span><span class="token punctuation">;</span>

Layout<span class="token punctuation">.</span><span class="token function">layout</span><span class="token punctuation">(</span>context<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="6"><li>渲染结果</li></ol> <img src="/minigame-canvas-engine/imgs/hello.jpg" width="300"></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/wechat-miniprogram/minigame-canvas-engine/edit/master/api/guide.md" target="_blank" rel="noopener noreferrer">在 GitHub 上编辑此页</a> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">3/15/2020, 8:10:26 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/minigame-canvas-engine/" class="prev router-link-active">
        概览
      </a></span> <span class="next"><a href="/minigame-canvas-engine/api/api.html">
        API
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"></div></div>
    <script src="/minigame-canvas-engine/assets/js/app.34da408a.js" defer></script><script src="/minigame-canvas-engine/assets/js/2.b58d2eb8.js" defer></script><script src="/minigame-canvas-engine/assets/js/7.6ddd2960.js" defer></script>
  </body>
</html>
